package com.humngyang.com.hmy.study.mywx.controller;


import com.humngyang.com.hmy.study.mywx.Util.RandomNum;
import com.humngyang.com.hmy.study.mywx.entity.ArticleNewsEntity;
import com.humngyang.com.hmy.study.mywx.entity.OutMesageEntity;
import com.humngyang.com.hmy.study.mywx.entity.InMessageEntity;
import com.humngyang.com.hmy.study.mywx.service.ValidateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
public class WxController {

    @Autowired
    ValidateService validateService;

    /**
     * Author : hmy
     *
     * @date 2020/3/19 21:13
     * describution : 接入验证
     **/
    @GetMapping("/wechat")
    @ResponseBody
    public String validate(String signature, String timestamp, String nonce, String echostr) {
        String validate = validateService.validate(signature, timestamp, nonce, echostr);
        return validate;
    }

    /**
     * Author : hmy
     *
     * @date 2020/3/19 21:14
     * describution : 处理普通消息
     **/
    @RequestMapping(value = "wechat", method = RequestMethod.POST, produces = {"application/xml; charset=UTF-8"})
    @ResponseBody
    public Object handleMessage(@RequestBody InMessageEntity ime) {
        System.out.println("ssssss" + ime.getMsgType());
        OutMesageEntity outMesageEntity = new OutMesageEntity();
        String contemtResp = "";

        outMesageEntity.setCreateTime(new Date().getTime());
        outMesageEntity.setFromUserName(ime.getToUserName());
        outMesageEntity.setToUserName(ime.getFromUserName());
        //判断消息类型
        if (ime.getMsgType().equals("text")) {
            String[] str = {"1、这辈子最疯狂的事，就是爱上了你，最大的希望，就是有你陪我疯一辈子。",
                    "都说流星有求必应，我愿在星空下等待，等到一颗星被我感动，载着我的思念和祝福，落在你熟睡的枕边。",
                    "前世，我将指环扣入你指间，你的胸口烙印着我的气息。今生，我寻你而来，梦绕魂牵，红丝缱绻，飞舞红尘，你在何方。环圈前世，只为许你今生的爱恋",
                    "你秀丽的脸庞吸引我，烂漫的眼睛牵着我，轻轻地问声：你可知道我在想什么？嘿嘿，在想你是否也已经爱上我。",
                    "当我终于停歇了靠近你的脚步，忽然发觉只要能够像最初那样遥望着你、倾听着你已经是足够。至于爱与不爱，交给时间去疑问吧。",
                    "白天有你就有梦，夜晚有梦就有你，你要好好照顾你自己，不要感冒流鼻涕；要是偶尔打喷嚏，那就代表我想你！",
                    "每一天都为你心跳，每一刻都被你感动，每一秒都为你担心。有你的感觉真好"};
            String content = ime.getContent();
            //关键字回复消息
            if (content.equals("你好")) {
                contemtResp = "你也好";
            } else if (content.contains("爱")) {
                Integer ranDomNum = RandomNum.getRanDomNum(0, str.length);
                String beauti = str[ranDomNum];
                contemtResp = beauti;
            } else {
                contemtResp = content;
            }
            //回复文本消息
            outMesageEntity.setMsgType("text");
            outMesageEntity.setContent(contemtResp);

        } else if (ime.getMsgType().equals("image")) {
            //回复图片消息
            outMesageEntity.setMsgType("image");
            outMesageEntity.setMediaId(new String[]{ime.getMediaId()});
        }else if (ime.getMsgType().equals("event")) {
            if(ime.getEvent().equals("subscribe")){
                //回复关注消息
//                outMesageEntity.setMsgType("text");
//                outMesageEntity.setContent("谢谢关注，[微笑]");
                //关注回复图文消息
                outMesageEntity.setMsgType("news");
                outMesageEntity.setArticleCount(1);
                ArticleNewsEntity item = new ArticleNewsEntity();
                item.setDescription("vivo手机，青春时尚");
                item.setPicUrl("http://imgcache.mysodao.com/img3/M04/46/A7/CgAPD1VQP8bTiQ12AADn_YOED-E646_900x0x1.JPG");
                item.setUrl("https://www.jd.com");
                item.setTitle("欢迎光临京东商城");
                outMesageEntity.setItems(new ArticleNewsEntity[]{item});
//                return outMesageEntity;
            }else if (ime.getEvent().equals("CLICK")){
                String eventKey = ime.getEventKey();
                if(eventKey.equals("premiere")){
                    outMesageEntity.setMsgType("text");
                    outMesageEntity.setContent("春天像一个小姑娘，不必精心装扮，" +
                            "便可清新可人，春像一个顽皮的孩子，阳光洒脱，充满活力，" +
                            "春天里，每一朵花开，我都细细聆听，每一缕新意，我都妥贴收藏。");
                }else if(eventKey.equals("myinfo")){
                    outMesageEntity.setMsgType("text");
                    outMesageEntity.setContent("天红云，满海金波，红日像一炉沸腾的钢水，喷薄而出，金光耀眼");
                }

            }
        }
        return outMesageEntity;
    }
}

